package beans;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class UserOfferBean {
	private int offerId;
	private String title;
	private String description;
	private Date offerStartDate;
	private Date offerEndDate;
	private double price;
	private int amountLeft;
	private String subcategory;
	private boolean event;
	private boolean discount;
	private String eventPlace;
	private Date eventStartDate;
	private String eventStartTime;
	private String eventEndTime;
	private double discountPrice;
	private Date discountEnds;
	
	int subcategoryId;
	BasicDataSource ds;
	
	public UserOfferBean(BasicDataSource ds) {
		this.ds = ds;
	}
	
	public void loadData(int offerId)
	{
		this.offerId = offerId;
		Connection cnn = null;
		Statement stat = null;
		ResultSet rs = null;
		
		try {
			cnn = ds.getConnection();
			stat = cnn.createStatement();
			String getOfferQuery = "SELECT idOffers, title, aboutOffer, offerStarts, offerEnds, price, amountLeft, offerSubcategory, isEvent,hasDiscount, eventPlace, eventStarts, eventStartTime" +
					", eventEndTime, discountEnds, discountPrice FROM offers WHERE idOffers = " + offerId;
			
			rs = stat.executeQuery(getOfferQuery);
			rs.next();
			{
				title = rs.getString("title");
				description = rs.getString("aboutOffer");
				
                java.sql.Date ossql = rs.getDate("offerStarts");
                offerStartDate = new Date(ossql.getTime());
                
                java.sql.Date oesql = rs.getDate("offerEnds");
                offerEndDate = new Date(oesql.getTime());
                
                price = rs.getDouble("price");
                amountLeft = rs.getInt("amountLeft");
                
                subcategoryId = rs.getInt("offerSubcategory");
                
                event = rs.getBoolean("isEvent");
                discount = rs.getBoolean("hasDiscount");
                
                if(event)
                {
                eventPlace = rs.getString("eventPlace");
                
                	java.sql.Date essql = rs.getDate("eventStarts");
                	eventStartDate = new Date(essql.getTime());
                
                	eventStartTime = rs.getString("eventStartTime");
                	eventEndTime = rs.getString("eventEndTime");
                }
                if(discount)
                {
                	java.sql.Date desql = rs.getDate("discountEnds");
                	discountEnds = new Date(desql.getTime());
                
                	discountPrice = rs.getDouble("discountPrice");
                }
			}
			
			stat.close();
			rs.close();
			
			stat = cnn.createStatement();
			String getSubcatName = "SELECT subcategoryName FROM offersubcategory WHERE idOfferSubcategory = " + subcategoryId;
			rs = stat.executeQuery(getSubcatName);
			rs.next();
			subcategory = rs.getString("subcategoryName");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			if(cnn != null)
			{
				try {
					cnn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
			if(stat != null)
			{
				try {
					stat.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	public int getOfferId() {
		return offerId;
	}
	public void setOfferId(int offerId) {
		this.offerId = offerId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Date getOfferStartDate() {
		return offerStartDate;
	}
	public void setOfferStartDate(Date offerStartDate) {
		this.offerStartDate = offerStartDate;
	}
	public Date getOfferEndDate() {
		return offerEndDate;
	}
	public void setOfferEndDate(Date offerEndDate) {
		this.offerEndDate = offerEndDate;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getAmountLeft() {
		return amountLeft;
	}
	public void setAmountLeft(int amountLeft) {
		this.amountLeft = amountLeft;
	}
	public String getSubcategory() {
		return subcategory;
	}
	public void setSubcategory(String subcategory) {
		this.subcategory = subcategory;
	}
	public boolean isEvent() {
		return event;
	}
	public void setEvent(boolean event) {
		this.event = event;
	}
	public boolean isDiscount() {
		return discount;
	}
	public void setDiscount(boolean discount) {
		this.discount = discount;
	}
	public String getEventPlace() {
		return eventPlace;
	}
	public void setEventPlace(String eventPlace) {
		this.eventPlace = eventPlace;
	}
	public Date getEventStartDate() {
		return eventStartDate;
	}
	public void setEventStartDate(Date eventStartDate) {
		this.eventStartDate = eventStartDate;
	}
	public String getEventStartTime() {
		return eventStartTime;
	}
	public void setEventStartTime(String eventStartTime) {
		this.eventStartTime = eventStartTime;
	}
	public String getEventEndTime() {
		return eventEndTime;
	}
	public void setEventEndTime(String eventEndTime) {
		this.eventEndTime = eventEndTime;
	}
	public double getDiscountPrice() {
		return discountPrice;
	}
	public void setDiscountPrice(double doscountPrice) {
		this.discountPrice = doscountPrice;
	}
	public Date getDiscountEnds() {
		return discountEnds;
	}
	public void setDiscountEnds(Date discountEnds) {
		this.discountEnds = discountEnds;
	}
	
	
	
}
